package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/21 11:14
 */
@RestController
@RequestMapping("/admin/dashboard")
@Slf4j
@CrossOrigin
public class DashBoardController {
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CategoryService categoryService;

    /**
     * "errno":0,
     * *     "data":{
     * *         "goodsTotal":426,
     * *         "userTotal":197,
     * *         "productTotal":8939,
     * *         "orderTotal":107
     * *     },
     * *     "errmsg":"成功"
     * * }
     *
     * @return
     */
    @GetMapping
    public Object DashBoard() {
        long UserCount = dtsUserService.selectUserCount();
        long goodsTotal = goodsService.selectCount();
        long productTotal = productService.selectCount();
        long orderTotal = orderService.selectCount();
        Map<String, Long> data = new HashMap<>();
        data.put("userTotal", UserCount);
        data.put("goodsTotal", goodsTotal);
        data.put("productTotal", productTotal);
        data.put("orderTotal", orderTotal);
        return ResponseUtil.ok(data);
    }


    /**
     * {
     * "errno":0,
     * "data":{
     * "categorySell":{
     * "categoryNames":["数码家电","日用百货","鞋帽箱包"],
     * "categorySellData":[
     * {"name":"数码家电","value":68.76},
     * {"name":"日用百货","value":48.76},
     * {"name":"鞋帽箱包","value":277.53}
     * ]
     * },
     * "orderAmts":{
     * "dayData":[
     * "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
     * "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
     * "2119-12-11","2119-12-13","2119-12-15","2119-12-16",
     * "2119-12-17","2119-12-19","2119-12-20","2119-12-23",
     * "2119-12-25","2119-12-27","2119-12-28","2119-12-29",
     * "2119-12-30"
     * ],
     * "orderAmtData":[
     * 6.88,138.79,277.65,65.64,182.00,61.00,
     * 289.01,375.76,29.50,247.50,145.50,
     * 31.18,399.44,16.99,150.01,218.65,255.77,
     * 126.59,275.25,97.74,14.63
     * ],
     * "orderCntData":[
     * 1,2,4,1,3,1,2,3,1,1,16,3,9,2,1,2,2,2,2,2,2
     * ]
     * },
     * "userOrderCnt":{
     * "dayData":[
     * "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
     * "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
     * "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
     * "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
     * "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
     * "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
     * "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
     * "2119-12-29","2119-12-30"
     * ],
     * "userCnt":[
     * 5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
     * 4,8,8,14,3,2,1,1,2,1,2,7,3,5
     * ],
     * "orderCnt":[
     * 1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
     * 0,2,1,0,0,2,0,2,0,2,2,2,2
     * ]
     * }
     * <p>
     * },
     * "errmsg":"成功"
     * }
     *
     * @return
     */
    @GetMapping("/chart")
    public Object chart() {

        int statisDayRange = 30;
        CategorySellVo categorySellVo = categoryService.selectCategory();
        List<DayStatis> dayStatisList = orderService.findOrderAmts(statisDayRange);
        String[] dayData = dayStatisList.stream().map(DayStatis::getDayStr).toArray(String[]::new);
        /**
         *    "orderAmts":{
         *             "dayData":[
         *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *                 "2119-12-11","2119-12-13","2119-12-15","2119-12-16",
         *                 "2119-12-17","2119-12-19","2119-12-20","2119-12-23",
         *                 "2119-12-25","2119-12-27","2119-12-28","2119-12-29",
         *                 "2119-12-30"
         *             ],
         *             "orderAmtData":[
         *                 6.88,138.79,277.65,65.64,182.00,61.00,
         *                 289.01,375.76,29.50,247.50,145.50,
         *                 31.18,399.44,16.99,150.01,218.65,255.77,
         *                 126.59,275.25,97.74,14.63
         *             ],
         *             "orderCntData":[
         *                 1,2,4,1,3,1,2,3,1,1,16,3,9,2,1,2,2,2,2,2,2
         *             ]
         *         },
         *
         *          *         "userOrderCnt":{
         *      *             "dayData":[
         *      *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *      *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *      *                 "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
         *      *                 "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
         *      *                 "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
         *      *                 "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
         *      *                 "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
         *      *                 "2119-12-29","2119-12-30"
         *      *             ],
         *      *             "userCnt":[
         *      *                 5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
         *      *                 4,8,8,14,3,2,1,1,2,1,2,7,3,5
         *      *             ],
         *      *             "orderCnt":[
         *      *                 1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
         *      *                 0,2,1,0,0,2,0,2,0,2,2,2,2
         *      *             ]
         */
        //用户数和日期
        List<DayStatis> dayStatisListByUser = dtsUserService.selectOrderCnt(statisDayRange);
        int[] ints = dayStatisListByUser.stream().mapToInt(DayStatis::getCnts).toArray();

        // dayStatisList.stream().
        HashSet<String> date = new HashSet<>();
        for (DayStatis dayStatis : dayStatisListByUser) {
            date.add(dayStatis.getDayStr());
        }
        for (DayStatis dayStatis : dayStatisList) {
            date.add(dayStatis.getDayStr());
        }
        ArrayList<String> dates = new ArrayList<>(date);
        Collections.sort(dates);
        String[] strings = dates.toArray(new String[dates.size()]);
        for (String string : strings) {
            System.out.println(string);
        }
        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        userOrderCntVo.setDayData(strings);

        HashMap<String, Integer> map = new HashMap<>();

        for (DayStatis dayStatis : dayStatisListByUser) {

            map.put(dayStatis.getDayStr(), dayStatis.getCnts());


        }
        Set<String> strings1 = map.keySet();


        int[] ints1 = new int[date.size()];
//         int i=0;
//        for (String s : strings) {
//            for (String s1 : strings1) {
//                if(s==s1){
//                    ints1[i++]=map.get(s1);
//                }
//            }
//        }
        for (int i = 0; i < strings.length; i++) {

            if (strings1.contains(strings[i])) {
                ints1[i] = map.get(strings[i]);
            } else {
                ints1[i] = 0;
            }


        }


        HashMap<String, Integer> map1 = new HashMap<>();
        for (DayStatis dayStatis : dayStatisList) {

            map1.put(dayStatis.getDayStr(), dayStatis.getCnts());


        }
        Set<String> strings2 = map1.keySet();

        int[] ints2 = new int[date.size()];
//        for (String s : strings) {
//            for (String s1 : strings2) {
//                if(s.equals(s1)){
//                    ints2[j++]=map1.get(s1);
//                    break;
//                }
//
//            }
//        }
        for (int i = 0; i < strings.length; i++) {

            if (strings2.contains(strings[i])) {
                ints2[i] = map1.get(strings[i]);
            } else {
                ints2[i] = 0;
            }


        }

        BigDecimal[] orderAmtData = dayStatisList.stream().map(DayStatis::getAmts).toArray(value -> new BigDecimal[dayStatisList.size()]);
        int[] orderCntData = dayStatisList.stream().mapToInt(DayStatis::getCnts).toArray();
        userOrderCntVo.setOrderCnt(ints2);
        userOrderCntVo.setUserCnt(ints1);

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayData);
        orderAmtsVo.setOrderAmtData(orderAmtData);
        orderAmtsVo.setOrderCntData(orderCntData);
        System.out.println(categorySellVo + "============================================================");
        HashMap<String, Object> data = new HashMap<>();
        data.put("categorySell", categorySellVo);
        data.put("orderAmts", orderAmtsVo);
        data.put("userOrderCnt", userOrderCntVo);
        return ResponseUtil.ok(data);
    }
}
